package com.qianya.system.face.web.patient.controller;

import com.qianya.model.pojo.patient.vo.MonthVo;
import com.qianya.model.pojo.patient.vo.PatientBoxRelevanceIndexVO;
import com.qianya.system.face.web.patient.service.IPatientBoxRelevanceService;
import com.qianya.system.face.web.patient.service.IPatientFileInfoService;
import com.qianya.system.face.web.patient.service.IPatientHisPrescriptionService;
import com.qianya.system.face.web.patient.service.IPatientOxygenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.qianya.model.common.CommonResult;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 患者绑定发药柜 前端控制器
 * </p>
 *
 * @author lhl
 * @since 2020-07-22
 */
@RestController
@RequestMapping("/patient/patient-box-relevance")
@Api(value = "PatientBoxRelevanceController", tags = "患者绑定发药柜接口")
@Slf4j
@Validated
public class PatientBoxRelevanceController {


    @Autowired
    private IPatientFileInfoService fileInfoService;
    @Autowired
    private IPatientHisPrescriptionService hisPrescriptionService;
    @Autowired
    private IPatientBoxRelevanceService boxRelevanceService;
    @Resource
    IPatientOxygenService iPatientOxygenService;

    /*@ApiOperation("服药异常列表")
    @GetMapping(value = "/eatException")
    public CommonResult eatException(){
        return boxRelevanceService.eatException();
    }*/
    @ApiOperation("首页统计")
    @GetMapping(value = "/index-statistics")
    public CommonResult<PatientBoxRelevanceIndexVO> indexStatistics() {

        List<MonthVo> list = hisPrescriptionService.eatStatistics();

        //12个月的每一个月的体征监测次数
        List<MonthVo> list1 = iPatientOxygenService.eatStatistics();

        //服药异常报告
        int eatException = boxRelevanceService.eatExceptionCount();

        //体征异常报告
        int signException = iPatientOxygenService.findsignsreport();

        // 服药总人次统计
        int eatCount = hisPrescriptionService.eatMedicCount(0);
        if (eatCount == 0) {
            eatCount = 1;
        }
        // 今日服药统计
        int eatTodayCount = hisPrescriptionService.eatMedicCount(1);


        //患者统计
        int patientCount = fileInfoService.patientCount(0);
        if (patientCount == 0) {
            patientCount = 1;
        }

        //今日新增患者
        int patientTodayCount = fileInfoService.patientCount(1);
        float newEat = 0f;
        if (eatCount == eatTodayCount) {
            newEat = 1f;
        } else {
            newEat = (float) eatTodayCount / (float) (eatCount - eatTodayCount);
        }

        float newPatient = 0f;
        if (patientCount == patientTodayCount) {
            newPatient = 1f;
        } else {
            newPatient = (float) patientTodayCount / (float) (patientCount - patientTodayCount);
        }

        PatientBoxRelevanceIndexVO pbrv = new PatientBoxRelevanceIndexVO();

        //服药异常报告
        pbrv.setEatException(String.valueOf(eatException));

        //体征报告异常
        pbrv.setSignException(String.valueOf(signException));

        // 今日服药统计
        pbrv.setNewPatient(String.valueOf(Math.round(newPatient * 100f)));

        //患者统计
        pbrv.setPatientCount(String.valueOf(patientCount));

        pbrv.setEatTodayCount(Integer.toString(eatTodayCount));

        pbrv.setNewEat(String.valueOf(Math.round(newEat * 100f)));

        // 服药总人次统计
        pbrv.setEatCount(list);

        //设置体征数据统计
        pbrv.setSignsCount(list1);


        return CommonResult.success(pbrv);
    }


    @ApiOperation("患者服药记录")
    @GetMapping(value = "/takeMedicineRecode")
    public CommonResult takeMedicineRecode(@ApiParam(value = "页数", required = true)
                                           @RequestParam(defaultValue = "1", name = "pageNo") int pageNo,
                                           @ApiParam(value = "页码", required = true)
                                           @RequestParam(defaultValue = "12", name = "pageSize") int pageSize,
                                           @ApiParam(value = "身份证号", required = true)
                                           @NotBlank(message = "身份证号不能为空")
                                           @RequestParam(defaultValue = "", name = "fileIdentityCard") String fileIdentityCard) {
        return boxRelevanceService.takeMedicineRecode(pageNo, pageSize, fileIdentityCard);
    }

    @ApiOperation("身份证查询服药异常记录 （日历格式）")
    @GetMapping(value = "/takeMedicineCalendarRecode")
    public CommonResult takeMedicineCalendarRecode(
            @ApiParam(value = "身份证号", required = true)
            @NotBlank(message = "身份证号不能为空")
            @RequestParam(defaultValue = "", name = "fileIdentityCard") String fileIdentityCard) {
        return boxRelevanceService.takeMedicineCalendarRecode(fileIdentityCard);
    }
    @ApiOperation("test")
    @GetMapping(value = "/test")
    public CommonResult test() {
        String[] str=new String[2];
        System.out.println(str[5]);
        return CommonResult.success();
    }

    @ApiOperation("解除预警")
    @GetMapping(value = "/cancelWarning")
    public CommonResult cancelWarning(
            @ApiParam(value = "医嘱id（brId）", required = true)
            @NotBlank(message = "医嘱id不能为空")
            @RequestParam(defaultValue = "", name = "医嘱id") String brId) {
        return boxRelevanceService.cancelWarning(brId);
    }

}
